import type { RouteRecordRaw } from 'vue-router'
import type { IUserMenu } from '@/service/login/type'
export function mapMenusToRoutes(userMenus: IUserMenu[]): RouteRecordRaw[] {
  const routes: RouteRecordRaw[] = []

  // 1. 先去加载默认所有的 routes
  const routeFiles = import.meta.globEager('../router/main/**/*.ts')

  const allRoutes: RouteRecordRaw[] = Object.values(routeFiles).map(
    (routeFile) => routeFile.default
  )

  // 2. 根据菜单获取需要添加的 routes
  const _recurseGetRoute = (menus: IUserMenu[]) => {
    for (const menu of menus) {
      if (menu.type === 2) {
        const route = allRoutes.find((route) => route.path === menu.url)
        if (route) {
          routes.push(route)
        }
      } else {
        _recurseGetRoute(menu.children)
      }
    }
  }

  _recurseGetRoute(userMenus)

  return routes
}
